package com.douyin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.douyin.dto.LikesDTO;
import com.douyin.dto.PageActionsDTO;
import com.douyin.dto.Result;
import com.douyin.entity.Likes;
import com.douyin.entity.User;
import com.douyin.entity.Videos;
import com.douyin.mapper.LikesMapper;
import com.douyin.mapper.VideosMapper;
import com.douyin.service.LikesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.douyin.service.VideosService;
import com.douyin.utils.BaseContext;
import com.douyin.vo.LikeVO;
import com.douyin.vo.UserActionVideoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 视频点赞表 服务实现类
 * </p>
 *
 * @author k
 * @since 2025-07-27
 */
@Service
public class LikesServiceImpl extends ServiceImpl<LikesMapper, Likes> implements LikesService {
    @Autowired
    LikesMapper likesMapper;
    @Autowired
    VideosMapper videoMapper;
    @Override
    public Result addLike(Long videoId) {
        Long currentId = BaseContext.getCurrentId();
        if (currentId == null) {
            return Result.fail("请先登录");
        }
        if (videoId == null) {
            return Result.fail("视频id不能为空");
        }
        Videos videos = videoMapper.selectById(videoId);
        if (videos == null) {
            return Result.fail("视频不存在");
        }
        LikeVO likeVO = new LikeVO();
        likeVO.setVideoId(videoId);
        likeVO.setUserId(currentId);
        //查询数据库获得点赞信息
        LambdaQueryWrapper<Likes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Likes::getVideoId, videoId).eq(Likes::getUserId, currentId);
        Likes likes1 = likesMapper.selectOne(queryWrapper);

        //判断是否已经点赞
        if (likes1 == null) {
            //点赞
            Likes likes2 = new Likes();
            likes2.setVideoId(videoId);
            likes2.setUserId(currentId);
            likes2.setStatus(1);
            likes2.setCreatedAt(LocalDateTime.now());
            likesMapper.insert(likes2);
            likeVO.setStatus(1);
            Integer likeCount = getLikeCount(videoId);
            likeVO.setLikeCount(likeCount);
            return Result.ok(likeVO);
        }
        if (likes1.getStatus() == 1) {
            //取消点赞
            likes1.setStatus(0);
            likesMapper.updateById(likes1);
            likeVO.setStatus(0);
            Integer likeCount = getLikeCount(videoId);
            likeVO.setLikeCount(likeCount);
            return Result.ok(likeVO);
        }
        if (likes1.getStatus() == 0) {
            //点赞
            likes1.setStatus(1);
            likesMapper.updateById(likes1);
            Integer likeCount = getLikeCount(videoId);
            likeVO.setLikeCount(likeCount);
            return Result.ok(likeVO);
        }
        return Result.fail("点赞状态异常");
    }

    @Override
    public Result getLikes(PageActionsDTO pageActionsDTO) {

        PageActionsDTO pageActionsDTO1 = checkFollowsDTO(pageActionsDTO);
        if (pageActionsDTO1 == null) {
            return Result.fail("参数错误");
        }
        IPage<UserActionVideoVO> likesPage = getLikesPage(pageActionsDTO1);
        return Result.ok(likesPage);

    }

    private Integer getLikeCount(Long videoId){
        LambdaQueryWrapper<Likes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Likes::getVideoId, videoId).eq(Likes::getStatus, 1);
        Integer count = likesMapper.selectCount(queryWrapper);
        Videos videos = new Videos();
        videos.setLikeCount(Long.valueOf(count));
        videos.setUpdatedAt(LocalDateTime.now());
        videos.setId(videoId);
        videoMapper.updateById(videos);
        return count;
    }
    private PageActionsDTO checkFollowsDTO(PageActionsDTO pageActionsDTO) {
        if (pageActionsDTO == null) {
            return null;
        }
        if (pageActionsDTO.getId() == null&&BaseContext.getCurrentId() == null) {
            return null;
        }
        if (BaseContext.getCurrentId() == null) {
            return null;
        }
        if (pageActionsDTO.getId() == null) {
            pageActionsDTO.setId(BaseContext.getCurrentId());
        }
        if (pageActionsDTO.getPageCount() == null) {
            pageActionsDTO.setPageCount(1);
        }
        if (pageActionsDTO.getPageSize() == null) {
            pageActionsDTO.setPageSize(10);
        }
        return pageActionsDTO;
    }
    public IPage<UserActionVideoVO> getLikesPage(PageActionsDTO queryDTO) {
        // 1. 创建 Mybatis-Plus 的 Page 对象
        IPage<UserActionVideoVO> page = new Page<>(queryDTO.getPageCount(), queryDTO.getPageSize());

        // 2. 调用 Mapper 的自定义方法，分页插件会自动生效
        IPage<UserActionVideoVO> resultPage = likesMapper.findUserLikesPage(page, queryDTO.getId());

        return resultPage;
    }
}
